/**
 * 
 */
package com.iuie.ns.web.html;

import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FilenameUtils;

import com.iuie.ns.frame.config.AppConfigUtil;

/**
 * @description HTML标签构造器
 * @author lj
 * @createTime 2016年8月23日 下午5:22:10
 */
public class SimpleHTMLTagBuilder {
	/**样式表*/
	public static final String STYLE_LINK_LEFT = "<link rel=\"stylesheet\" href=\"";
	/**脚本*/
	public static final String SCRIPT_LEFT = "<script type=\"text/javascript\" src=\"";
	public static final String RES_PATH = AppConfigUtil.getInstance().getProperty("res.path");
	
	private String cssPath = RES_PATH+"css/";
	private String jsPath = RES_PATH+"js/";
	
	public static final String TAG_RIGHT_NO_BODY = "\" />\n";
	public static final String TAG_RIGHT_JS = "\"></script>\n";
	
	public static final String CSS="CSS";
	public static final String JS="JS";
	
	private StringBuilder sb;

	private String contextPath;
	
	public SimpleHTMLTagBuilder(String contextPath){
		this.contextPath = contextPath;
		sb = new StringBuilder(12);
	}
	
	public SimpleHTMLTagBuilder(){
		this(null);
	}
	
	/**
	 * @param {@link #cssPath}
	 */
	public void setCssPath(String cssPath) {
		this.cssPath = cssPath;
	}
	/**
	 * @param {@link #jsPath}
	 */
	public void setJsPath(String jsPath) {
		this.jsPath = jsPath;
	}
	
	public SimpleHTMLTagBuilder buildStyleLinkTag(String fileName) {
		sb.append(STYLE_LINK_LEFT);
		sb.append(contextPath);
		if (!fileName.startsWith("libs/")) {
			sb.append(cssPath);
		} else {
			sb.append(RES_PATH);
		}
		sb.append(fileName);
		sb.append(TAG_RIGHT_NO_BODY);
		return this;
	}
	/**
	 * 
	 * @description 根据 fileName 文件名后缀判断生成哪种标签
	 * @author lj
	 * @date 2016年8月23日 下午5:54:14
	 * @param fileName 文件名
	 * @return
	 */
	public SimpleHTMLTagBuilder build(String fileName){
		String suffix = null;
		if ((suffix = FilenameUtils.getExtension(fileName))!=null) {
			if(suffix.equalsIgnoreCase(CSS)) {
				buildStyleLinkTag(fileName);
			} else if(suffix.equalsIgnoreCase(JS)){
				buildJavascriptTag(fileName);
			}
		}
		return this;
	}
	/**
	 * 
	 * @description 根据文件名列表来构建HTML标签字符串
	 * @author lj
	 * @date 2016年8月28日 下午2:04:57
	 * @param fileNameLst 文件名列表
	 * @param type 筛选的文件类型，为null时不进行筛选
	 * @return HTMLTagBuilder
	 */
	public SimpleHTMLTagBuilder buildMultipleHtmlTag(Collection<String> fileNameLst,String type){
		if (fileNameLst==null || fileNameLst.size()==0) {
			return this;
		}
		for(String fileName:fileNameLst){
			String suffix = FilenameUtils.getExtension(fileName);
			if(suffix==null) break;
			if (type==null) {
				build(fileName);
			} else if (type.equalsIgnoreCase(CSS)) {
				if(suffix.equalsIgnoreCase(CSS))
					buildStyleLinkTag(fileName);
			} else if(type.equalsIgnoreCase(JS)){
				if(suffix.equalsIgnoreCase(JS))
					buildJavascriptTag(fileName);
			}
		}
		return this;
	}
	/**
	 * 
	 * @description {@link #buildMultipleHtmlTag(List, String)}
	 * @author lj
	 * @date 2016年8月28日 下午1:00:12
	 * @param fileNameLst
	 * @return
	 */
	public SimpleHTMLTagBuilder buildMultipleHtmlTag(List<String> fileNameLst){
		return buildMultipleHtmlTag(fileNameLst, null);
	}
	/**
	 * @description build javascript tag base on fileName
	 * @author lj
	 * @date 2016年8月23日 下午5:40:42
	 * @param fileName
	 */
	public SimpleHTMLTagBuilder buildJavascriptTag(String fileName) {
		sb.append(SCRIPT_LEFT);
		sb.append(contextPath);
		if (!fileName.startsWith("libs/")) {
			sb.append(jsPath);
		} else {
			sb.append(RES_PATH);
		}
		sb.append(fileName);
		sb.append(TAG_RIGHT_JS);
		return this;
	}

	@Override
	public String toString() {
		return sb.toString();
	}

}
